﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Baosight.ZNH.ZMCS.UI.Utils;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 初始数据管理
    /// </summary>
    /// Title:ZMCS24
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 路涛
    /// Version: 1.0
    /// History:
    ///       2010-12-14  路涛   [创建]
    ///       2011-05-24  张小飞 [修改新增的模式为弹出]
    ///
    public partial class ZMCS31 : EFViewCmes
    {
        private InitialDataBlock block = new InitialDataBlock();//构造前台下拉列表

        public ZMCS31()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 从表查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);
            base.OnHyperlinkButtonClickPopup(eiInfo, "数据明细-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 从表新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            string _value = this.fdInitialDataId.Value == null ? "" : this.fdInitialDataId.Value.ToString();

             if (_value.Equals("") || _value.Equals("NULL"))
            {
                base.OpenEFWindowAlert("请先选择一条或者录入一条初始数据列表中的记录！", true);
                return;
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                base.OnInsertButtonClickPopup(eiInfo, "数据明细-新增", "insert", null, sender, e);
            }
        }

        /// <summary>
        /// 从表修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            if (this.runType.Value == null || this.runType.Value.ToString() == "1")
            {
                string _fdType = _list[0]["fdType"].ToString();
                if (_fdType != null && _fdType.Equals("系统"))
                {
                    base.OpenEFWindowAlert("系统数据，不能修改！", true);
                    return;
                }
            }

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "数据明细-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 从表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 从表删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            if (this.runType.Value == null || this.runType.Value.ToString() == "1")
            {
                IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();
                foreach (EIRow _row in _list)
                {
                    string _fdType = _row["fdType"].ToString();
                    if (_fdType != null && _fdType.Equals("系统"))
                    {
                        base.OpenEFWindowAlert("系统数据，不能删除！", true);
                        return;
                    }
                }
            }

            base.OnDeleteButtonClick( sender, e);
        }

        /// <summary>
        /// 从表点击启用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStartButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("ZMCS31", "modifyStart", "在用", sender, e);
        }

        /// <summary>
        /// 从表点击停用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStopButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("ZMCS31", "modifyStop", "停用", sender, e);
        }

        /// <summary>
        /// 从表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            InitialDataDetails _details = new InitialDataDetails(_dialogType);//构造一个新的修改页面
            _details.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(block.GetStatusBlock());
            if (null != this.runType.Value && "1".Equals(this.runType.Value.ToString()) && !"detail".Equals(_dialogType))
            {
                _eIInfo.AddBlock(block.GetRunTypeBlock());
            }
            else
            {
                _eIInfo.AddBlock(block.GetTypeBlock());
            }
            _details.EIDataContext.Update(_eIInfo);

            if (_dialogType == "insert")
            {
                _details.fdInitialDataId.Value = this.fdInitialDataId.Value;//主表ID
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _details;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }
}
